<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/7
 * Time: 13:54
 */
namespace app\mtadminapi\controller;

use app\mtadminapi\model\MtAdminSysAccount;
use app\mtadminapi\model\MtOrder;
use app\util\ReturnCode;
use think\Request;

/**
 * 后台管理系统订单
 * Class AdminSysOrder
 * @package app\mtadminapi\controller
 */
class AdminSysOrder extends Base
{
    public function __construct(Request $request){
        parent::__construct($request);
    }

    /**
     * 后台管理订单查询
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryOrder(){
        $adminID = intval($this->request->param("adminID")); // 管理员ID
        $page = $this->request->get("p") ? intval($this->request->get("p")) : 1; // 页码
        $limit = $this->request->get("limit") ? intval($this->request->get("limit")) : config("apiadmin.ADMIN_LIST_DEFAULT"); // 每页条数
        /**
         * @todo
         */
//        $adminAuthority = (new MtAdminSysAccount())->where([["adminID","=",$adminID],["status","=","1"]])->field("authority")->find(); // 管理员权限组
//        if(!$adminAuthority){
//            return $this->bindError(ReturnCode::DATA_EXISTS,"管理员权限组不存在");
//        }
        if($this->request->get("start")){ // 查询的开始时间
            $start = strtotime($this->request->get("start"));
            if(!$this->request->get("end")){
                $timeWhere[] = ["orderCreateTime",['>=',$start],['<=',time()]];
            }else{
                $end = strtotime($this->request->get("end"));
                $timeWhere[] = ["orderCreateTime",['>=',$start],['<=',$end]]; // 查询的结束时间
            }
        }else{
            $timeWhere = " 1=1 ";
        }
        // 订单状态
        if($this->request->get("orderState")){
            $orderState = $this->request->get("orderState");
        }else{
            $orderState = " 1=1 ";
        }
//        if($adminAuthority->authority == 1){
//            $res = $this->superAdminOrder($adminID,$page,$limit,$timeWhere,$orderState);
//        }else{
//            $res = $this->businessAdminOrder($adminID,$page,$limit,$timeWhere,$orderState);
//        }
        $res = $this->businessAdminOrder($adminID,$page,$limit,$timeWhere,$orderState);
        return $res;
    }

    /**
     * 超级管理员订单查询
     * @param $adminID
     * @param $page
     * @param $limit
     * @param $timeWhere
     * @param $orderState
     * @return array|\PDOStatement|string|\think\Collection|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function superAdminOrder($adminID,$page,$limit,$timeWhere,$orderState){
        if(!$adminID){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"请检查adminID");
        }
        $obj = new MtOrder();
        $businessOrder = $obj
            ->where($timeWhere)
            ->page($page,$limit)
            ->where($orderState)
            ->select();
        if($businessOrder){
            return $this->bindSuccess("查询成功",$businessOrder,"");
        }
        return $businessOrder;
    }

    /**
     * 商家管理员订单
     * @param $adminID
     * @param $page
     * @param $limit
     * @param $timeWhere
     * @param $orderState
     * @return array|\PDOStatement|string|\think\Collection|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function businessAdminOrder($adminID, $page, $limit,$timeWhere,$orderState){
        if(!$adminID){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"请检查adminID");
        }
        $shopID = (new MtAdminSysAccount())->where([["adminID","=",$adminID],["status","=","1"]])->field("shopID")->find();
        if(!$shopID){
            return $this->bindError(ReturnCode::DATA_EXISTS,"商户ID不存在");
        }
        $obj = new MtOrder();
        $businessOrder = $obj
            ->where("shopID","=",$shopID->shopID)
            ->where($timeWhere)
            ->page($page,$limit)
            ->where($orderState)
            ->select();
        if($businessOrder){
            return $this->bindSuccess("查询成功",$businessOrder,"");
        }
        return $businessOrder;
    }

    public function saveOrder(){
        $orderID = $this->request->post("orderID");
        if(!$orderID){
            return $this->bindError(ReturnCode::EMPTY_PARAMS,"缺少参数orderID");
        }
        $res = (new MtOrder())->save($orderID);
        if(!$res){
            return ; // @todo
        }
    }
}